﻿using System;
using System.Collections.Generic;
using System.Linq;
#if !NETCOREAPP
using System.Web;
using System.Web.Mvc;
using System.Web.WebPages;
    using System.Web.Mvc.Html;

#endif

#if NETCOREAPP
using Microsoft.AspNetCore.Mvc.Rendering;
using Kugar.Core.Web;
#endif
using Kugar.MVC.MobileUI.WeUI.Helpers;

namespace Kugar.MVC.MobileUI.WeUI.PageControls.PageForm
{
    public class PageForm_Selector:PageForm_FieldBase
    {
        private List<SelectorItem> _lst = new List<SelectorItem>(5);
        private string _selectorValue = "";
        private string _selectorName = "";

        public PageForm_Selector(ViewContext viewContext) : base(viewContext)
        {
            //_attrs.Class("weui-cell_select");
        }

        public PageForm_Selector FirstItem(string title, string value)
        {
            _lst.Insert(0, new SelectorItem(title, value));

            return this;
        }

        public PageForm_Selector Options(IEnumerable<SelectorItem> options)
        {
            _lst.AddRange(options);

            return this;
        }

        public PageForm_Selector Options<T>(IEnumerable<T> source, Func<T, string> titleFunc,
            Func<T, string> valueFunc)
        {
            _lst.Clear();

            _lst.AddRange(OptionHelper.Convert(source, titleFunc, valueFunc));

            return this;
        }

        public PageForm_Selector Options(Type enumType)
        {
            _lst.Clear();

            _lst.AddRange(OptionHelper.Convert(enumType));

            return this;
        }



        public PageForm_Selector Value(string value)
        {
            _selectorValue = value;

            return this;
        }

        
        protected override void RenderBody()
        {
            ViewContext.Writer.WriteLine($"<select class=\"weui-select\" name=\"{getName()}\">");

            foreach (var item in _lst)
            {
                ViewContext.Writer.WriteLine($"<option {(string.IsNullOrWhiteSpace(_selectorValue) ? "" : "selected=\"selected\"")}  value=\"{item.Value}\">{item.Title}</option>");
            }

            ViewContext.Writer.WriteLine("</select>");
        }
    }
}